package com.ccjq.shosestore.web.controller.order;

import com.ccjq.shosestore.core.domain.PageInfo;
import com.ccjq.shosestore.order.po.CarAndProductPo;
import com.ccjq.shosestore.order.po.OrderPo;
import com.ccjq.shosestore.order.service.impl.OrderServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;


public class OrderServlet extends HttpServlet {
    private static final String FALG_ZHIFU="zhifu";//结算购物车
    private static final String FALG_SELECT = "selectorder";//查询订单
    private static final String FALG_SELECTORDER= "selectorder_id";//根据订单id查询具体数据
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("userId");//用户的登录信息（id）
        String falg = request.getParameter("falg");
        System.out.println(falg);
        OrderServiceImpl orderImpl = new OrderServiceImpl();
        if(StringUtils.isNotBlank(userId)){
                if(FALG_ZHIFU.equals(falg)){
                    String uuid = orderImpl.uuid();//订单ID
                    int result = orderImpl.insertGuanLian(uuid,userId);
                    System.out.println("result="+result);
                    if(result>0){
                        int det = orderImpl.deleteCar(userId);
                        if(det>0){
                        java.util.Date  date=new java.util.Date();
                        java.sql.Date  sqldate=new java.sql.Date(date.getTime());//生成时间
                        SimpleDateFormat ss = (SimpleDateFormat)DateFormat.getDateTimeInstance();
                        ss.applyPattern("yyyy-MM-dd HH:mm:ss");
                        String time = ss.format(sqldate);
                        System.out.println(time);
                        int payment = Integer.parseInt(request.getParameter("payment"));//状态码
                        double amount = Double.parseDouble(request.getParameter("amount"));//总金额
                        String receiver = request.getParameter("receiver");//收货人姓名
                        String province = request.getParameter("province");//省份
                        String city = request.getParameter("city");//城市
                        String county = request.getParameter("county");//区县
                        String address = request.getParameter("address");//地址
                        String zipcode = request.getParameter("zipcode");//邮编
                        String telphone = request.getParameter("telphone");//电话
                        //user_id//用户id
                        OrderPo orderPo = new OrderPo();
                        orderPo.setOrder_id(uuid);
                        orderPo.setCreate_time(time);
                        orderPo.setPayment(payment);
                        orderPo.setAmonut(amount);
                        orderPo.setReceiver(receiver);
                        orderPo.setProvince(province);
                        orderPo.setCity(city);
                        orderPo.setCounty(county);
                        orderPo.setAddress(address);
                        orderPo.setZipcode(zipcode);
                        orderPo.setTelphone(telphone);
                        orderPo.setUser_id(userId);
                        int res = orderImpl.insertXinXi(orderPo);
                            if(res>0){
                                PageInfo<OrderPo> pageInfo = orderImpl.selectFengYeAll(userId,6,1);
                                List<OrderPo> listorder = pageInfo.getObjs();
                                request.setAttribute("pageInfo",pageInfo);
                                request.setAttribute("listorder",listorder);
                                request.getRequestDispatcher("/WEB-INF/jsp/order/order_list.jsp").forward(request,response);
                            }
                        }
                    }
                }else if(FALG_SELECT.equals(falg)){
                    PageInfo<OrderPo> pageInfo = null;
                    int pageSizes = 0;
                    int pageNos = 0;
                    String pageSize   = request.getParameter("pageSize");//每页显示的行数
                    String pageNo     = request.getParameter("pageNo"); //当前页
                    if( StringUtils.isBlank(pageSize)){
                        pageSizes=6;
                    }else {
                        pageSizes =Integer.parseInt(pageSize);
                    }
                    if(StringUtils.isBlank(pageNo)){
                        pageNos=1;
                    }else {
                        pageNos = Integer.parseInt(pageNo);
                    }
                    System.out.println("pageSizes:"+pageSizes+";pageNos:"+pageNos);
                    pageInfo = orderImpl.selectFengYeAll(userId,pageSizes,pageNos);
                    request.setAttribute("pageInfo",pageInfo);
                    request.getRequestDispatcher("/WEB-INF/jsp/order/order_list.jsp").forward(request,response);

                }else if(FALG_SELECTORDER.equals(falg)){
                    String name = null;
                    String order_id = request.getParameter("order_id");
                    if(StringUtils.isNotBlank(order_id)){
                        OrderPo orderPo = orderImpl.selectOrder(order_id);
                        int payment= orderPo.getPayment();
                        //double amonut = orderPo.getAmonut();
                        if(payment == 1){
                            name = "微信支付";
                        }else {
                            name = "支付宝支付";
                        }
                        List<CarAndProductPo> listOrder = orderImpl.selectorder(order_id);
                        request.setAttribute("order_id",order_id);//订单id
                        request.setAttribute("name",name);//支付类型
                        //request.setAttribute("amonut",amonut);//总金额
                        request.setAttribute("orderPo",orderPo);
                        request.setAttribute("listOrder",listOrder);
                        request.getRequestDispatcher("/WEB-INF/jsp/order/order_detail.jsp").forward(request,response);
                    }

                }


        }else{
            request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
